package com.example.idachuappone.cook.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.cook.action.CookAction;
import com.example.idachuappone.cook.entity.CookResult;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.ComUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

public class CookTwoAdapter extends BaseAdapter {
	private String breed;
	private BitmapUtils bu;
	private Context context;
	private CookAction cookAction;
	private List<CookResult> list;
	private String location;

	private class ViewHolder {
		ImageView img_green;
		ImageView img_star;
		TextView tv_bespeak_num;
		TextView tv_breed;
		TextView tv_distance;
		TextView tv_name;
		TextView tv_service_address;

		private ViewHolder() {
		}
	}

	public CookTwoAdapter(Context context, BitmapUtils bu) {
		this.context = context;
		this.bu = bu;
		this.cookAction = new CookAction();
	}

	public void setBreed(String breed) {
		this.breed = breed;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public void setList(List<CookResult> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return this.list == null ? 0 : this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return this.list == null ? null : (CookResult) this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LinearLayout.inflate(this.context, R.layout.simple_cook_plv_item_two, null);
			viewHolder = new ViewHolder();
			viewHolder.img_green = (ImageView) convertView.findViewById(R.id.img_green);
			viewHolder.img_star = (ImageView) convertView.findViewById(R.id.img_star);
			viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewHolder.tv_breed = (TextView) convertView.findViewById(R.id.tv_breed);
			viewHolder.tv_service_address = (TextView) convertView.findViewById(R.id.tv_service_address);
			viewHolder.tv_bespeak_num = (TextView) convertView.findViewById(R.id.tv_bespeak_num);
			viewHolder.tv_distance = (TextView) convertView.findViewById(R.id.tv_distance);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		CookResult cookResult = this.list.get(position);
		viewHolder.tv_name.setText(cookResult.getName());
		viewHolder.tv_bespeak_num.setText(cookResult.getBespeak_num() + "\u4eba\u9884\u8ba2\u8fc7");
		viewHolder.tv_service_address.setText(cookResult.getService_for());
		viewHolder.tv_breed.setText(this.cookAction.getBreed(this.breed, cookResult.getTags()));
		String str = "";
		if (this.location == null || this.location.length() <= 0) {
			str = "\u65e0\u6cd5\u83b7\u53d6\u672c\u5730\u4f4d\u7f6e";
		} else {
			str = ComUtil.getTwoSum(cookResult.getLocation(), this.location);
		}
		viewHolder.tv_distance.setText(str);
		this.bu.display(viewHolder.img_green, cookResult.getRecipe().get(0).getImg_small(),
				new BitmapLoadCallBack<View>() {
					@Override
					public void onLoadCompleted(View view, String arg1, Bitmap bitmap, BitmapDisplayConfig arg3,
							BitmapLoadFrom arg4) {
						((ImageView) view).setImageBitmap(BitmapUtilHelper.toRoundCorner(bitmap,
								BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY));
						if (bitmap.isRecycled()) {
							bitmap.recycle();
						}
					}

					@Override
					public void onLoadFailed(View arg0, String arg1, Drawable arg2) {
					}
				});
		if (Double.valueOf(cookResult.getStar()).doubleValue() > 4.0d) {
			viewHolder.img_star.setImageResource(R.drawable.icon_star05);
		} else if (Double.valueOf(cookResult.getStar()).doubleValue() > 3.0d) {
			viewHolder.img_star.setImageResource(R.drawable.icon_star04);
		} else if (Double.valueOf(cookResult.getStar()).doubleValue() > 2.0d) {
			viewHolder.img_star.setImageResource(R.drawable.icon_star03);
		} else if (Double.valueOf(cookResult.getStar()).doubleValue() > 1.0d) {
			viewHolder.img_star.setImageResource(R.drawable.icon_star02);
		} else {
			viewHolder.img_star.setImageResource(R.drawable.icon_star01);
		}
		return convertView;
	}
}
